import { _decorator, Component, instantiate, Node, Pool, Prefab, ProgressBar, Sprite, SpriteFrame, v3 } from 'cc';
import { ShiBingDt } from '../../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { ResMgr } from '../../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { Emitter } from '../../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { UIManager } from '../../../FrameWork3.8.6/FrameWork/UIFramework/UIManager';
import { ShiBingLayer } from '../ModuleLayer/ShiBingLayer';

const { ccclass, property } = _decorator;

@ccclass('ShiBingProgress')
export class ShiBingProgress extends Component {
    //当前关卡的兵种数据数组
    private _arrShiBingDt:ShiBingDt[]=[];

    //通过当前兵种传过来的数据确定当前进度值停在哪里
    private _maxProgress:number=0;
    //兵种按钮预制体
    private _ShiBingBtnNodePool:Pool<Node>=null;

    //管理当前生成的所有兵种按钮的容器，方便销毁
    private _arrShiBingBtn:Node[]=[];

    //兵种进度条
    private _progressBar:ProgressBar=null;

    //每秒跑多少进度
    private _degree:number=600/30;
    //可以出几种类型的兵
    private _num:number=0;
    onInit(arrShiBingDt:ShiBingDt[]){
        this._arrShiBingDt=arrShiBingDt;
        //计算最大进度,目前最大是3
        this._maxProgress=arrShiBingDt.length/3;
        //初始化进度条
        this._progressBar=this.getComponent(ProgressBar);
        //注册出兵的点击事件
        let startBingBtn=this.node.getChildByName("_FireBtn");
        startBingBtn.on("click",()=>{
            if(this._progressBar.progress<1/3){
                this._num=0;
            }
            else{
                if(this._progressBar.progress>=1/3&&this._progressBar.progress<2/3){
                    this._num=1;
                    //需要对第一个按钮进行操作，将他的照片变换
                    //this._arrShiBingBtn[0].getComponent(Sprite).spriteFrame
                }
                else{
                        if(this._progressBar.progress<1){
                            this._num=2;
                        }
                        else{
                            this._num=3;
                        }
                    }
                    this.fireBing();
                }
        },this)

        //获取到预制体
        let shiBingPrefab=ResMgr.instance.getRes<Prefab>(BundleConfig.Game.name,"ShiBingBtn");
        //定义对象池
        this._ShiBingBtnNodePool=new Pool<Node>(()=>{
            let node=instantiate(shiBingPrefab);
            return node;
        },3,(node:Node)=>{
            node.destroy();
        });

        //获取到父物体
        let ShiBingBtnParent=this.node.getChildByName("ShiBingBtnParent");
        //通过数据生成几个按钮
        for(let ShiBingDt of arrShiBingDt){
            let index=parseInt(ShiBingDt.id.toString().charAt(3));
            //从对象池中拿出物体
            let shiBingNode=this._ShiBingBtnNodePool.alloc();
            //设置父亲与图片
            shiBingNode.parent=ShiBingBtnParent;
            //shiBingNode.getComponent(Sprite).spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,ShiBingDt.iconName);
            //添加到容器中方便管理
            this._arrShiBingBtn.push(shiBingNode);
            //设置位置
            shiBingNode.setPosition(v3(200*(index-1),shiBingNode.position.y));
        }


    }

    //出兵的方法
    fireBing(){
        this._progressBar.progress=0;
        //呼叫兵种管理器生成玩家士兵
        let shiBingLayerNode=UIManager.instance.getUI("GameUI").node.getChildByName("ShiBingLayer")
        let shiBingLayerTs=shiBingLayerNode.getComponent(ShiBingLayer);
        Emitter.instance.emit("addPlayerShiBing",shiBingLayerTs,this._arrShiBingDt,this._num);
    }


    //兵种进度条的自更新方法
    onUpdate(dt:number){
        this._progressBar.progress+=dt*this._degree/600;
        if(this._progressBar.progress>=this._maxProgress){
            this._progressBar.progress=this._maxProgress;
        }
    }
}


